package com.weiweb.action;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;

import weibo4j.model.User;
import weibo4j.org.json.JSONException;
import weibo4j.org.json.JSONObject;

import com.weiweb.common.SessionConstant;
import com.weiweb.domain.Task;
import com.weiweb.service.ISinaWeiboService;
import com.weiweb.service.ITaskService;
import com.weiweb.service.IUserService;

/**
 * 任务中心
 * 
 */
public class TaskCenterAction extends BaseAction {
	private String uid;
	public ISinaWeiboService sinaWeiboService;
	public IUserService userService;
	public ITaskService taskService;
	private List<Task> taskList;
	private static final Logger log = Logger.getLogger(TaskCenterAction.class);
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 * @return
	 */
	public String execute() {
		taskList=taskService.findTaskList();
		return "success";
	}

	/**
	 * 加关注
	 * 
	 * @return
	 */
	//@Transactional
	public String addFollow() {
		com.weiweb.domain.User sessionUser=(com.weiweb.domain.User)session.get(SessionConstant.SESSION_USER);
		User addedUser = sinaWeiboService.followByUid(sessionUser.getAccessToken(), uid);
		boolean result = true;
		if (null != addedUser) {
			try {
				response.setContentType("text/html;charset=UTF-8");
				/**
				 * 用户增加推广币
				 */
				com.weiweb.domain.User paramUser1=new com.weiweb.domain.User();
				paramUser1.setSinaUid(sessionUser.getSinaUid());
				com.weiweb.domain.User queryUser1=userService.findSinaUserByUid(paramUser1);
				if(null==queryUser1){
					log.error("未找到用户："+sessionUser.getSinaUid());
				}
				int addScore=Integer.parseInt(queryUser1.getScore())+10;
				queryUser1.setScore(String.valueOf(addScore));
				userService.updateUser(queryUser1);
				session.put(SessionConstant.SESSION_USER, queryUser1);
				log.info("用户"+paramUser1.getSinaUid()+"关注"+uid+"获得10个推广币");
				/**
				 * 被关注用户扣除推广币
				 */
				com.weiweb.domain.User paramUser2=new com.weiweb.domain.User();
				paramUser2.setSinaUid(uid);
				com.weiweb.domain.User queryUser2=userService.findSinaUserByUid(paramUser2);
				if(null==queryUser2){
					log.error("未找到用户："+uid);
				}
				int takeOffScore=Integer.parseInt(queryUser2.getScore())-10;
				queryUser2.setScore(String.valueOf(takeOffScore));
				userService.updateUser(queryUser2);
				log.info("用户"+uid+"被"+paramUser1.getSinaUid()+"关注，扣除10个推广币");
				
				JSONObject json = new JSONObject();
				json.put("result", result);
				log.info(json.toString());
				response.getWriter().print(json);
				log.info("关注结果：" + result);
			} catch (IOException e) {
				log.error("IO异常!" + e.getMessage(), e);
				result = false;
				try {
					response.getWriter().print(result);
				} catch (IOException e1) {
					log.error("IO异常2！" + e);
				}
			} catch (JSONException e) {
				log.error("JSON数据转换异常！",e);
			}
		}
		return null;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public ISinaWeiboService getSinaWeiboService() {
		return sinaWeiboService;
	}

	public void setSinaWeiboService(ISinaWeiboService sinaWeiboService) {
		this.sinaWeiboService = sinaWeiboService;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public ITaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(ITaskService taskService) {
		this.taskService = taskService;
	}

	public List<Task> getTaskList() {
		return taskList;
	}

	public void setTaskList(List<Task> taskList) {
		this.taskList = taskList;
	}
	
	
}
